import * as echarts from "echarts";

const nowSize = function (size) {
  return size / (1920 / document.body.clientWidth);
};
const deepClone = function (source) {
  if (!source && typeof source !== "object") {
    throw new Error("error arguments", "deepClone");
  }
  const targetObj = source.constructor === Array ? [] : {};
  Object.keys(source).forEach((keys) => {
    if (source[keys] && typeof source[keys] === "object") {
      targetObj[keys] = deepClone(source[keys]);
    } else {
      targetObj[keys] = source[keys];
    }
  });
  return targetObj;
};
// 折线面积图颜色渐变
const getLineAreaStyle = (colorStart, colorEnd) => {
  return {
    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
      { offset: 0, color: colorStart },
      { offset: 1, color: colorEnd }
    ])
  };
};
/**
 *  生成UUID
 * @param withSeparator 是否有分割符
 * @returns {string}
 */
const generateUUID = (withSeparator = true) => {
  let d = new Date().getTime();
  if (window.performance && typeof window.performance.now === "function") {
    d += performance.now();
  }
  const tpl = withSeparator
    ? "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
    : "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx";
  return tpl.replace(/[xy]/g, function (c) {
    const r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
  });
};

export { nowSize, deepClone, getLineAreaStyle, generateUUID };
